//
//  MainTabBarController.m
//  Weibo
//
//  Created by wpapple on 16/5/30.
//  Copyright © 2016年 红红火火恍恍惚惚嘿嘿嘿. All rights reserved.
//

#import "MainTabBarController.h"
#import "UIView+XL.h"
#import "TabBarButton.h"
#import "MainTabBar.h"
#import "EditingView.h"
#import "TextViewController.h"

@interface MainTabBarController ()

@property (strong , nonatomic) TabBarButton * tabBarButton;

@end

@implementation MainTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.selectedIndex = 0;
    
//    [self setTabBarView];
    
    
    
    [self setTabBarValue];
    
//    [self setANewTabBarView];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)setTabBarValue{
    UIStoryboard * sb1 = [UIStoryboard storyboardWithName:@"IndexController" bundle:[NSBundle mainBundle]];
    UIViewController * index = [sb1 instantiateInitialViewController];
    
    UIStoryboard * sb2 = [UIStoryboard storyboardWithName:@"MessageController" bundle:[NSBundle mainBundle]];
    UIViewController * index2 = [sb2 instantiateInitialViewController];
    
    UIViewController * index3 = [[UIViewController alloc]init];
    
    UIStoryboard * sb4 = [UIStoryboard storyboardWithName:@"DiscoverController" bundle:[NSBundle mainBundle]];
    UIViewController * index4 = [sb4 instantiateInitialViewController];
    
    UIStoryboard * sb5 = [UIStoryboard storyboardWithName:@"MeController" bundle:[NSBundle mainBundle]];
    UIViewController * index5 = [sb5 instantiateInitialViewController];
    
    self.viewControllers = @[index,index2,index3,index4,index5];
    
    self.tabBar.tintColor = [UIColor orangeColor];
    
    self.tabBar.items[2].enabled = NO;
    
    UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(self.tabBar.width / 5 * 2, 0, self.tabBar.width / 5, self.tabBar.height)];
    
    [button setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(touchAddButton) forControlEvents:UIControlEventTouchUpInside];
    
    [self.tabBar addSubview:button];
    
//    UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(self.tabBar.width / 5 * 2, 0, self.tabBar.width / 5 , self.tabBar.height)];
//    [button setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
//    [button setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
//    UIButton * aButton = (UIButton *)self.tabBar.items[2];
//    
//    aButton = button;
    
    
    
    
}

-(void)touchAddButton{
    
    EditingView * editingView = [EditingView EditingViewNib];
    [editingView setBackgroundColor:[UIColor colorWithWhite:1 alpha:0.9]];
    [editingView.textButton addTarget:self action:@selector(touchTextButton) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:editingView];
    [editingView.sendMessageButton addTarget:self action:@selector(touchEditingViewButton) forControlEvents:UIControlEventTouchUpInside];
    
    
}

-(void)touchTextButton{
    
   
    
}

-(void)setTabBarView{
    
    UIView * view = [[UIView alloc]initWithFrame:self.tabBar.frame];
    CGFloat width = 30;
    CGFloat margin = (view.width - 30 * 4 - view.height)/6;
    NSArray * arrNormal = @[@"tabbar_home",@"tabbar_message_center",@"",@"tabbar_discover",@"tabbar_profile"];
    NSArray * arrSelected = @[@"tabbar_home_selected",@"tabbar_message_center_selected",@"",@"tabbar_discover_selected",@"tabbar_profile_selected"];
    NSArray * title = @[@"首页",@"消息",@"",@"发现",@"我"];
    
    for (int i = 0; i < 5; i++) {
        
        if (i == 2) {
            UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(margin + i * (margin + width), 0, view.height, view.height)];
            [button setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
            [button setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
            [view addSubview:button];
        }else{
        
            TabBarButton * button = [[TabBarButton alloc]initWithFrame:CGRectMake(margin + i * (margin + width), 0, width, view.height)];
            if (i > 2) {
                CGFloat space = view.height - width;
                button.x += space;
            }
            [button setImage:[UIImage imageNamed:arrNormal[i]] forState:UIControlStateNormal];
            [button setImage:[UIImage imageNamed:arrSelected[i]] forState:UIControlStateSelected];
            [button setTitle:title[i] forState:UIControlStateNormal];
            [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            [button setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
            [button addTarget:self action:@selector(touchTabBarButton:) forControlEvents:UIControlEventTouchUpInside];
            if (i > 2) {
                button.tag = i - 1;
            }else{
                button.tag = i;
            }
            
            if (i == 0) {
                self.tabBarButton = button;
                button.selected = YES;
            }
            
            [view addSubview:button];
        }
    }
    
    
    
    [self.tabBar removeFromSuperview];
    [self.view addSubview:view];
}

-(void)touchTabBarButton:(TabBarButton *)sender{
    self.tabBarButton.selected = NO;
    sender.selected = YES;
    self.selectedIndex = sender.tag;
    self.tabBarButton = sender;
}

-(void)setANewTabBarView{
    MainTabBar * mainTabBar = [[MainTabBar alloc]init];
    mainTabBar.frame = self.tabBar.frame;
    [self.tabBar removeFromSuperview];
    [self.view addSubview:mainTabBar];
    
    UIStoryboard * sb1 = [UIStoryboard storyboardWithName:@"IndexController" bundle:[NSBundle mainBundle]];
    UIViewController * index = [sb1 instantiateInitialViewController];
    
    UIStoryboard * sb2 = [UIStoryboard storyboardWithName:@"MessageController" bundle:[NSBundle mainBundle]];
    UIViewController * index2 = [sb2 instantiateInitialViewController];
    
    UIViewController * index3 = [[UIViewController alloc]init];
    
    UIStoryboard * sb4 = [UIStoryboard storyboardWithName:@"DiscoverController" bundle:[NSBundle mainBundle]];
    UIViewController * index4 = [sb4 instantiateInitialViewController];
    
    UIStoryboard * sb5 = [UIStoryboard storyboardWithName:@"MeController" bundle:[NSBundle mainBundle]];
    UIViewController * index5 = [sb5 instantiateInitialViewController];
    
    self.viewControllers = @[index,index2,index3,index4,index5];

}

-(void)touchEditingViewButton{
    
    UIStoryboard * sb = [UIStoryboard storyboardWithName:@"TextViewController" bundle:nil];
    
    TextViewController * textViewController = [sb instantiateInitialViewController];
    
    [self presentViewController:textViewController animated:YES completion:nil];
    
}

@end
